package com.zhaotianhao.controller;

import com.zhaotianhao.common.AjaxResult;
import com.zhaotianhao.pojo.Files;
import com.zhaotianhao.service.FilesService;
import com.zhaotianhao.util.FileUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @Author Z-7
 * @Date 2022/3/2
 */
@RestController
@RequestMapping("file")
public class FileController {
    @Resource
    private FileUtils fileUtils;
    @DubboReference
    private FilesService filesService;
    @RequestMapping("upload")
    public AjaxResult upload(MultipartFile file){
        // 文件路径
        String upload = fileUtils.upload(file);
        if(StringUtils.hasText(upload)){
            Files files = new Files();
            files.setPath(upload);
            Files newFiles = filesService.insert(files);
            System.out.println(newFiles);
            return new AjaxResult("200","",newFiles);
        }
        return new AjaxResult("500","上传失败","");
    }
}
